package com.baor.vip.service.impl;

import com.baor.common.core.domain.R;
import com.baor.vip.domain.ScoreChangeDTO;
import com.baor.vip.domain.ScoreChangeVO;
import com.baor.vip.domain.ScoreVO;
import com.baor.vip.mapper.ScoreMapper;
import com.baor.vip.service.ScoreService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baor.common.utils.PageUtils.startPage;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public Map<String,Object> selectScoreVOList(ScoreChangeDTO scoreChangeDTO) {
        ScoreChangeVO scoreChangeVO = scoreMapper.totalScore();
        startPage();
        List<ScoreChangeDTO> scoreChangeList = scoreMapper.scoreList(scoreChangeDTO);
        PageInfo<ScoreChangeDTO> pageInfo = new PageInfo<>(scoreChangeList);

        Map<String,Object> map = new HashMap<>();
        map.put("scoreChangeVO",scoreChangeVO);
        map.put("scoreChangeList",pageInfo);
        return map;
    }

    //积分来源下拉框
    @Override
    public List<String> scoreOrginBox() {
        return scoreMapper.scoreOrginBox();
    }

    @Override
    public int addScore(ScoreChangeVO scoreChangeVO) {
        int ok1 = 0;
        int ok2 = 0;
        if(scoreChangeVO.getVipUid() != null){
           ok1 = scoreMapper.addScoreChange(scoreChangeVO);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("vipUid",scoreChangeVO.getVipUid());
        map.put("scoreChange",scoreChangeVO.getScoreChange());
         if(ok1 > 0){
             if(scoreMapper.select(scoreChangeVO.getVipUid()) == 1){
                 ok2 = scoreMapper.updateVipScore(map);
             }else {
                 ok2 = scoreMapper.addVipScore(map);
             }
         }
         if(ok1 == 1 && ok2 == 1){
             return 1;
         }
        return 0;
    }
}
